package com.junxonline.plat.dao.repository;

import com.junxonline.plat.dao.model.Role;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: RoleRepository
 * @Description: 角色数据操作类
 * @author JunX
 */
@Repository
public interface RoleRepository extends JpaRepository<Role, Long>, JpaSpecificationExecutor<Role> {

    Role findByRoleKey(String roleKey);

    Role findByRoleId(Long roleId);

    List<Role> findByRoleIdIn(Long[] roleIds);

    @Query(value = "SELECT user_role.* FROM sys_user_role user_role WHERE user_role.role_id = ?1", nativeQuery = true)
    List<Map<String, BigInteger>> findUserRolesByRoleId(Long roleId);

    @Modifying
    @Query(value ="DELETE FROM sys_role_menu WHERE menu_id = ?1 AND role_id = ?2", nativeQuery = true)
    void deleteRoleMenuIn(Long menuId, Long roleId);

    @Modifying
    @Query(value ="DELETE FROM sys_user_role WHERE user_id = ?1 AND role_id = ?2", nativeQuery = true)
    void deleteUserRoleIn(Long userId, Long roleId);

    @Modifying
    @Query(value = "INSERT INTO sys_role_menu (menu_id, role_id) VALUES(?1, ?2)", nativeQuery = true)
    void saveRoleMenuIn(Long menuId, Long roleId);

}
